use std::fmt::{Debug, Display};

use ch10_trait::{NewsArticle, Summary, Tweet};
fn main() {
    let comm = NewsArticle {
        title: String::from("test"),
        author: String::from("lsx"),
        content: String::from("hello,comment"),
        pub_date: String::from("2021-04-17 21:56:00"),
    };
    println!("{}", comm.summarize());

    let t = Tweet {
        username: String::from("lishixi"),
        content: String::from("xxxx"),
        reply: false,
        retweet: false,
    };
    println!("{}", t.summarize());

    println!("summarize_author={}", t.summarize_author());
    //trait 作为参数
    notify(&t);
    //允许传入实现summary trait的任何类型
    notify1(&comm, &t);
    // 只能传相同具体类型
    notify2(&comm, &comm);
}

fn notify(item: &impl Summary) {
    println!("notify = {}", item.summarize());
}
/// 允许传入实现summary trait的任何类型
fn notify1(item1: &impl Summary, item2: &impl Summary) {
    println!(
        "notify1 item1={}, item2={}",
        item1.summarize(),
        item2.summarize()
    )
}
// item1和item2 具体类型必须相同
fn notify2<T: Summary>(item1: &T, item2: &T) {
    println!(
        "notify1 item1={}, item2={}",
        item1.summarize(),
        item2.summarize()
    )
}
#[allow(dead_code)]
#[allow(unused_variables)]
fn some_fun<T: Display + Debug, U: Clone>(p1: &T, p2: &U) {
    println!("通过 + 指定多个 trait bound");
}

#[allow(dead_code)]
#[allow(unused_variables)]
fn some_fun2<T, U>(p1: &T, p2: &U)
where
    T: Display + Debug,
    U: Clone,
{
    println!("通过 where 简化 trait bound");
}

///返回实现了 trait 的类型
#[allow(dead_code)]
fn some_fun3() -> impl Summary {
    Tweet {
        username: String::from("horse_ebooks"),
        content: String::from("of course, as you probably already know, people"),
        reply: false,
        retweet: false,
    }
}
// 编译不过
// fn returns_summarizable(switch: bool) -> impl Summary {
// if switch {
//     NewsArticle {
//         title: String::from("test"),
//         author: String::from("lsx"),
//         content: String::from("hello,comment"),
//         pub_date: String::from("2021-04-17 21:56:00"),
//     }
// } else {
//     Tweet {
//         username: String::from("horse_ebooks"),
//         content: String::from("of course, as you probably already know, people"),
//         reply: false,
//         retweet: false,
//     }
// }
// }
